package com.liujit.pay.wx.domain.params.order;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import com.liujit.pay.wx.enums.H5Type;

/**
 * @description: H5场景信息
 * @author: liujun
 * @create: 2021-02-20 11:02
 */
public class OrderH5Info {

    /**
     * 场景类型
     */
    private H5Type h5Type;

    /**
     * 应用名称
     */
    private String appName;

    /**
     * 网站URL
     */
    private String appUrl;

    /**
     * OS平台BundleID
     */
    private String bundleId;

    /**
     * Android平台PackageName
     */
    private String packageName;

    OrderH5Info(H5Type h5Type, String appName, String appUrl, String bundleId, String packageName) {
        this.h5Type = h5Type;
        this.appName = appName;
        this.appUrl = appUrl;
        this.bundleId = bundleId;
        this.packageName = packageName;
    }

    /**
     * 初始化参数
     *
     * @param h5Type {@link H5Type} 场景类型
     * @return
     */
    public static OrderH5Info.Builder builder(H5Type h5Type) {
        return new OrderH5Info.Builder(h5Type);
    }

    public static class Builder {

        private H5Type h5Type;
        private String appName;
        private String appUrl;
        private String bundleId;
        private String packageName;

        Builder(H5Type h5Type) {
            this.h5Type = h5Type;
        }

        /**
         * 设置应用名称
         *
         * @param appName 应用名称
         * @return
         */
        public OrderH5Info.Builder appName(String appName) {
            this.appName = appName;
            return this;
        }

        /**
         * 设置网站URL
         *
         * @param appUrl 网站URL
         * @return
         */
        public OrderH5Info.Builder appUrl(String appUrl) {
            this.appUrl = appUrl;
            return this;
        }

        /**
         * 设置iOS平台BundleID
         *
         * @param bundleId iOS平台BundleID
         * @return
         */
        public OrderH5Info.Builder bundleId(String bundleId) {
            this.bundleId = bundleId;
            return this;
        }

        /**
         * 设置Android平台PackageName
         *
         * @param packageName Android平台PackageName
         * @return
         */
        public OrderH5Info.Builder packageName(String packageName) {
            this.packageName = packageName;
            return this;
        }

        public OrderH5Info build() {
            return new OrderH5Info(this.h5Type, this.appName, this.appUrl, this.bundleId, this.packageName);
        }
    }

    /**
     * 转化JSON字符串
     *
     * @return
     */
    public String toJSONString() {
        JSONObject json = new JSONObject();
        // 场景类型
        json.set("type", h5Type.getType());
        // 应用名称
        if (StrUtil.isNotBlank(appName)) {
            json.set("app_name", appName);
        }
        // 网站URL
        if (StrUtil.isNotBlank(appUrl)) {
            json.set("app_url", appUrl);
        }
        // iOS平台BundleID
        if (StrUtil.isNotBlank(bundleId)) {
            json.set("bundle_id", bundleId);
        }
        // Android平台PackageName
        if (StrUtil.isNotBlank(packageName)) {
            json.set("package_name", packageName);
        }
        return json.toString();
    }
}
